package com.ruoyi;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.SysUser2;
import com.ruoyi.domain.SysUserRole2;
import com.ruoyi.mapper.FaUserMapper;
import com.ruoyi.mapper.SysUser2Mapper;
import com.ruoyi.mapper.SysUserRole2Mapper;
import com.ruoyi.service.FaUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


//@RequestMapping("/ray/user/")
@RestController
@Api(tags = "用户")
@RequestMapping(value = "/ray/user/")
public class User {

    @Autowired
    private FaUserService faUserService;

    @Autowired
    private FaUserMapper faUserMapper;

    @Autowired
    private SysUser2Mapper sysUser2Mapper;

    @Autowired
    private SysUserRole2Mapper sysUserRole2Mapper;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456

    @PostMapping("login")
    @ApiOperation("登陆")
    public AjaxResult login(String username, String password) {



        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            return AjaxResult.error("密码不在指定范围内");
//            throw new UserPasswordNotMatchException();
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            return AjaxResult.error("用户名不在指定范围内");
//            throw new UserPasswordNotMatchException();
        }

        QueryWrapper<SysUser2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);

        SysUser2 sysUser = this.sysUser2Mapper.selectOne(queryWrapper);

        if(sysUser.getUserId()!=null){
            System.out.printf(String.valueOf(sysUser));
        }else{
            return AjaxResult.error("没有该账号");
        }


//
//        List user;
//        try {
//            user = Db.use().find(Entity.create("sys_user").set("user_name", "xiaohe"));
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        if(!user.isEmpty()){
////            passwordService.validate(user);
//        }
//        System.out.println("查询的数据");
//        System.out.printf(String.valueOf(user));
////        passwordService.validate(user);
//        // IP黑名单校验
//        FaUser faUser = new FaUser();
//        FaUserMapper faUserMapper;
//        faUser.setNickname("xiaohe");
//        faUser.setUsername("xiaohe");
//        faUser.setPassword("123456");
//
//          int res = this.faUserMapper.insert(faUser);
//        System.out.printf(String.valueOf(res));
////        FaUserMapper faUserMapper = new FaUserMapper() {
////        };
//
        boolean $是否相同 =  SecurityUtils.matchesPassword(password,sysUser.getPassword());
        if($是否相同){
            StpUtil.login(sysUser.getUserId());
            Map<String, Object> user = new HashMap<>();
            QueryWrapper role_qw = new QueryWrapper<>();
            role_qw.eq("user_id",sysUser.getUserId());

            SysUserRole2 role = sysUserRole2Mapper.selectOne(role_qw);
            user.put("role_id",role.getRoleId());
            user.put("id",sysUser.getUserId());
            user.put("username",sysUser.getUserName());
            user.put("nickName",sysUser.getNickName());
            user.put("avatar",sysUser.getAvatar());
            user.put("token",StpUtil.getTokenValue());
            return AjaxResult.success("登录成功",user);
        }else {
            return AjaxResult.error("账号或密码错误");
        }


    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
//    @RequestMapping(value = "isLogin")
    @ApiOperation("是否登陆")
    @GetMapping("isLogin")
    public AjaxResult isLogin() {
        if(StpUtil.isLogin()){
            return AjaxResult.success("登录成功",StpUtil.getTokenInfo());
        }else {
            return AjaxResult.error("未登录");
        }
    }
}
